<?php

class C_Action 
{
	protected $_request = null;
	protected $_response = null;
	protected $_suffix = '';
	protected $_module = '';
	protected $_action = '';
	protected $_method = '';
	protected $_IntercptorMap = '';
	public function __construct($request, $response)
	{
		$this->_request = $request;
		$this->_response = $response;
		$this->_IntercptorMap =   include load_interceptor_map();
	}
	public function dispatch($doMethod)
	{
		$this->_module = $this->_request->getformatModule();
		$this->_action = $this->_request->getformatAction();
		$this->_method = $this->_request->getformatMethod();
		
		if(!method_exists($this, $doMethod))
		{
			
			$this->_response->dispachError();
		}
		$InterceptorArray = $this->getInterceptor();
		foreach($InterceptorArray as $Interceptor)
		{
			$Interceptor->_begin();
		}
		
		$this->init();
		$this->$doMethod();
		
		foreach($InterceptorArray as $Interceptor)
		{
			$Interceptor->_end();
		}
		if(!$this->_response->isRedirect())
		{
			$this->render();
		}
		
	}
	public function startAction($module,  $action,  $method)
	{
		$this->_response->startAction($module,  $action,  $method);
		exit;
	}
	public function RedirectJump($url)
	{
		$this->_response->RedirectJump($url);
	}
	public function jumpToErrorPage($error_info)
	{
		
		$module = strtolower($this->_module);
		
		$jumpurl = $this->getUrl($module, 'error','error',array('error_info'=>$error_info));
	
		$this->RedirectJump($jumpurl);
	}
	public function getInterceptor()
	{
		$InterceptorArray = array();
		$MM = $this->_IntercptorMap[$this->_module];
		if(!empty($MM))
		{
			foreach ((array)$MM['Allow'] as $MI)
			{
			    	$denyA = (array)$MM['Deny'][$this->_action];
			    	$denyM = (array)$MM[$this->_action]['Deny'][$this->_method];
					if(!in_array($MI, $denyA) && !in_array($MI, $denyM))
					{
						if(!array_key_exists ($MI, $InterceptorArray))
						{
							include_once load_interceptor($MI);
							$InterceptorArray[$MI] = new $MI($this->_request, $this->_response);
						}
					}
			}
			
			$MA = $MM[$this->_action];
			
			if(!empty($MA))
			{
				foreach ((array)$MA['Allow'] as $MI)
				{
				    	$denyA = (array)$MA['Deny'][$this->_method];
						if(!in_array($MI, $denyA))
						{
							if(!array_key_exists ($MI, $InterceptorArray))
							{
								include_once load_interceptor($MI);
								$InterceptorArray[$MI] = new $MI($this->_request, $this->_response);
							}
						}
				}
				$M = $MA[$this->_method];
				
				if(!empty($M))
				{
					foreach ((array)$M as $MI)
					{
							if(!array_key_exists ($MI, $InterceptorArray))
							{
								include_once load_interceptor($MI);
								$InterceptorArray[$MI] = new $MI($this->_request, $this->_response);
							}
					}
				}
			}
		}
		return $InterceptorArray;
	}
	public function render()
	{
		$templatePath = load_template_path($this->_module, $this->_action, $this->_method);
		$view = new C_View($this->_response, $this->_request->getApplicationRoot());
		
		$view->initView($templatePath, $this->_response->getParams());
		$view->render();
		
	}
	public function getUrl($module,  $action,  $method,  $params = array(),$query = array())
	{
		return $this->_response->getUrl($module,  $action,  $method,  $params, $query);
	}
	public function init(){}
}

?>